$().ready(function() {

});
function chk_del_diary(){
    $( ".result-message-box" ).dialog({
    	title: "Delete Diary",
    	autoOpen: false,
    	minHeight: 140,
		modal: true,
		draggable: false ,
		show: {
			effect: "explode",
			duration: 300
		},
		hide: {
			effect: "explode",
			duration: 300
		},	      
		buttons: {
			"Yes": function() {
				delete_diary();
				$(this).dialog('destroy').remove();
				html='<div class="result-message-box">&nbsp;</div>';
				$('.box-two-content').append(html);
			},
			"No": function() {
				$(this).dialog("close");
			}
		}
      });
    $('.ui-dialog-content').text("Are you sure?");
	$( ".result-message-box" ).dialog("open");	
}
function delete_diary(){
	var intID = $("#intDiaryID").val();
	if(intID!=""){
			xfunc = "diary/delete_diary";
			data_sent = {
					'intID':intID
			};
			
			$.ajax({ 
				type: "POST", // request type POST
				url: base_url+xfunc, // post to PHP file
				data: data_sent, // request send to PHP file
				dataType:'json',
				success: function(result){ // result = return from php request		
					//$('.result-message-box').html('<span>'+result.message+'</span>');
					$('.diary-btn-list').click();
				    $( ".result-message-box" ).dialog({
				    	title: "Delete Diary",
				    	autoOpen: false,
				    	minHeight: 140,
						modal: true,
						draggable: false ,
						show: {
							effect: "explode",
							duration: 300
						},
						hide: {
							effect: "explode",
							duration: 300
						},	      
						buttons: {
							"Ok": function() {
								$(this).dialog("close");
								$(this).dialog('destroy').remove();
								html='<div class="result-message-box">&nbsp;</div>';
								$('.box-two-content').append(html);								
							}
						}
				      });
				    $('.ui-dialog-content').text(result.message);
				    $( ".result-message-box" ).dialog("open");	
				}
			});	
	}
}
function save_diary(){
	var intID = $("#intDiaryID").val();
	var strImgLink = $("#strImgLinkDiary").val();
	var strTitle = $("#title-journal-diary").val();
	var strNotes = $("#notes-journal-diary").val();
	var intUserID = $.cookie('session_code');	

	xfunc = "diary/save_diary";
	data_sent = {
			'intID':intID,
			'strImgLink':strImgLink,
			'strTitle':strTitle,
			'strNotes':strNotes,
			'intUserID':intUserID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request		
			//$('.result-message-box').html('<span>'+result.message+'</span>');
			$('.diary-btn-list').click();
		    $( ".result-message-box" ).dialog({
		    	title: "Save Diary",
		    	autoOpen: false,
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
						$(this).dialog('destroy').remove();
						html='<div class="result-message-box">&nbsp;</div>';
						$('.box-two-content').append(html);						
					}
				}
		      });
		    $('.ui-dialog-content').text(result.message);
		    $( ".result-message-box" ).dialog("open");				
		}
	});		
}
function list_diary(){
	var intUserID = $.cookie('session_code');

	xfunc = "diary/list_diary";
	data_sent = {
			'intUserID':intUserID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			html='<lu>';
			html+='<li class="rowt" id="rowt"><div class="col1">Title</div><div class="col2">Notes</div></li>';
			var c = 1;
			tgle_class = 'row1';
			$(result.strNotes).each(function(k,v){
				if(tgle_class == 'row2'){
					tgle_class = 'row1';
				} else {
					tgle_class = 'row2';
				}
				strActive='';

				html+='<li id="checkuplog_list_'+result.intID[k]+'" class="'+tgle_class+' '+strActive+' row-checklupog-list">';
					html+='<a onclick="get_diary('+result.intID[k]+')"><div class="col1">'+result.strTitle[k]+'</div><div class="col2">'+result.strNotes[k]+'</div></a>';
				html+='</li>';
				c++;
			});
			html+='</lu>';
			$('.list-diary-notes').html(html);
		}
	});	
}
function get_diary(intID){
	xfunc = "diary/get_diary";
	data_sent = {
			'intID':intID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			$('.list-diary-box').fadeOut(1000,function(){
				block = $('.journal-diary-box').css("display");
				if(block=="none"){
					$('.journal-diary-box').show();
				}				
				$('.wrapper-img-diary').hide();
				$('.wrapper-list-diary').hide();
				$('.wrapper-journal-diary').show();
			});	
			html = '<img class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+result.strImgLink+'" />';
			$("#strImgLinkDiary").val(result.strImgLink);
			$(".img-diary").html(html);			
			$("#intDiaryID").val(result.intID);			
			$("#title-journal-diary").val(result.strTitle);			
			$("#notes-journal-diary").val(result.strNotes);
		}
	});		
}
function diary_clear(opt){
	$('.img-diary-box').show();
	$('.upload-diary-box').show();
	$('.wrapper-img-diary').hide();
	//$('.wrapper-share-diary').hide();
	$('.wrapper-journal-diary').hide();
	$('.wrapper-list-diary').show();
	$(".img-diary").html('');
	$("#title-journal-diary").val('');
	$("#notes-journal-diary").val('');
	$("#intDiaryID").val('');
	$("#strImgLinkDiary").val('');	
	if(opt!=1){
		$('.result-message-box').html('');
		block = $('.journal-diary-box').css('display');
		if(block=="none"){
			$('.journal-diary-box').show();
		}
		$('.wrapper-journal-diary').show();
		$('.wrapper-list-diary').hide();		
	}
}
function diary(){
	html='';
	html+='<div class="wrapper-driary">';
		html+='<div class="wrapper-img-diary hide">';
			html+='<div class="img-diary-box img-rounded">';
				html+='<div class="img-diary img-rounded"></div>';
			html+='</div>';
			html+='<div class="upload-diary-box">';
				html+='<a id="fileupload"  class="fileinput-button" title="Upload image">';
					html+='<input type="file" name="files" class="fileupload" id="input_fileupload"/>';
				html+='</a>';
				html+='<a class="diary-btn-next" title="Next"></a>';
			html+='</div>';
		html+='</div>';
		html+='<div class="wrapper-journal-diary hide">';
			html+='<div class="journal-diary-box img-rounded">';
				html+='<div class="journal-diary-title">';
					html+='Title:&nbsp;<input type="text" id="title-journal-diary" />';
					html+='<i class="clearfix"></i>';
					html+='<textarea id="notes-journal-diary"></textarea>';
				html+='</div>';
				html+='<div class="journal-diary-control">';
					html+='<a class="journal-diary-btn-prev" title="Photo">&nbsp;</a>';
					html+='<a class="journal-diary-btn-save" title="Save">&nbsp;</a>';
					html+='<a class="journal-diary-btn-delete" title="Delete">&nbsp;</a>';
					//html+='<a class="journal-diary-btn-share" title="Share">&nbsp;</a>';
					html+='<a class="diary-btn-list" title="List">&nbsp;</a>';
				html+='</div>';
			html+='</div>';
		html+='</div>';	
		html+='<div class="wrapper-list-diary hide">';
			html+='<div class="list-diary-box img-rounded">';
				html+='<div class="list-diary">';
					html+='<div class="list-diary-notes"></div>';
				html+='</div>';
				html+='<div class="list-diary-control">';
					html+='<a class="diary-btn-new" title="New"></a>';
					//html+='<a class="list-diary-btn-prev" title="Previous">&nbsp;</a>';
				html+='</div>';
			html+='</div>';
		html+='</div>';		
	html+='</div>';
	html+='<div class="result-message-box">&nbsp;</div>';	
	$('.box-two-content').html(html);
	$('.box-two .title span').text("Diary");
	diary_clear(1);
	set_fileupload();
	switchwindow('diary');	
	bind_diary_func();
}
function bind_diary_func(){
	list_diary();
	$('.diary-btn-next').click(function(){
		$('.img-diary-box').hide("explode",{ pieces: 16 },1000);
		$('.upload-diary-box').fadeOut(1000, function(){
			$('.wrapper-img-diary').hide();
			$('.wrapper-journal-diary').fadeIn(1000);
			$('.journal-diary-box').show();
		});
	});
	$('.diary-btn-list').click(function(){
		$('.img-diary-box').hide("fast");
		$('.upload-diary-box').fadeOut("fast", function(){
			$('.wrapper-img-diary').hide();
			$('.wrapper-journal-diary').hide();
			$('.wrapper-list-diary').show();
			$('.list-diary-box').fadeIn("fast");
			list_diary();
		});
	});
	$('.journal-diary-btn-prev').click(function(){
		$('.journal-diary-box').fadeOut(1000,function(){	
			$('.wrapper-img-diary').show();
			$('.wrapper-journal-diary').hide();
			$('.img-diary-box').show("explode",{ pieces: 16 },1000,function(){
				$('.upload-diary-box').fadeIn(1000);
			});
		});
	});
	$('.list-diary-btn-prev').click(function(){
		$('.list-diary-box').fadeOut(1000,function(){	
			$('.wrapper-img-diary').show();
			$('.wrapper-list-diary').hide();
			$('.img-diary-box').show("explode",{ pieces: 16 },1000,function(){
				$('.upload-diary-box').fadeIn(1000);
			});
		});
	});	
	$(".journal-diary-btn-save").click(function(){
		save_diary();
	});
	$(".diary-btn-new").click(function(){
		diary_clear(0);
	});
	$(".journal-diary-btn-delete").click(function(){
		chk_del_diary();
	});
	
//	$(".journal-diary-btn-share").click(function(){
//		$(".wrapper-share-diary").dialog({
//			minHeight: 140,
//			modal: true,
//			show: {
//				effect: "explode",
//					duration: 1000
//				},
//				hide: {
//				effect: "explode",
//					duration: 1000
//				},
//			draggable: false
//			});
//	});
}
function diary_nav(){
	var chk_log = $.cookie('chk_log');	
	if(chk_log==1){	
		html='';
		html+='<div class="wrapper-driary">';
			html+='<div class="wrapper-img-diary hide">';
				html+='<div class="img-diary-box img-rounded">';
					html+='<div class="img-diary img-rounded"></div>';
				html+='</div>';
				html+='<div class="upload-diary-box">';
					html+='<a id="fileupload"  class="fileinput-button" title="Upload image">';
						html+='<input type="file" name="files" class="fileupload" id="input_fileupload"/>';
					html+='</a>';
					html+='<a class="diary-btn-next" title="Next"></a>';
				html+='</div>';
			html+='</div>';
			html+='<div class="wrapper-journal-diary hide">';
				html+='<div class="journal-diary-box img-rounded">';
					html+='<div class="journal-diary-title">';
						html+='Title:&nbsp;<input type="text" id="title-journal-diary" />';
						html+='<i class="clearfix"></i>';
						html+='<textarea id="notes-journal-diary"></textarea>';
					html+='</div>';
					html+='<div class="journal-diary-control">';
						html+='<a class="journal-diary-btn-prev" title="Photo">&nbsp;</a>';
						html+='<a class="journal-diary-btn-save" title="Save">&nbsp;</a>';
						html+='<a class="journal-diary-btn-delete" title="Delete">&nbsp;</a>';
						//html+='<a class="journal-diary-btn-share" title="Share">&nbsp;</a>';
						html+='<a class="diary-btn-list" title="List">&nbsp;</a>';
					html+='</div>';
				html+='</div>';
			html+='</div>';			
//			html+='<div class="wrapper-share-diary hide" title="Share your journal">';
//				html+='<div class="share-diary-box">';
//					html+='<ul>';
//						html+='<li><a><i class="fb-icon"></i>Facebook</a></li>';
//						html+='<li><a><i class="twit-icon"></i>Twitter</a></li>';
//						html+='<li><a><i class="email-icon"></i>Email</a></li>';
//						html+='<li>&nbsp;</li>';
//					html+='</ul>';
//				html+='</div>';
//			html+='</div>';	
			html+='<div class="wrapper-list-diary hide">';
				html+='<div class="list-diary-box img-rounded">';
					html+='<div class="list-diary">';
						html+='<div class="list-diary-notes"></div>';
					html+='</div>';
					html+='<div class="list-diary-control">';
						html+='<a class="diary-btn-new" title="New"></a>';
						//html+='<a class="list-diary-btn-prev" title="Previous">&nbsp;</a>';
					html+='</div>';
				html+='</div>';
			html+='</div>';		
		html+='</div>';
		html+='<div class="result-message-box">&nbsp;</div>';	
		$('.box-two-content').html(html);
		$('.box-two .title span').text("Diary");
		diary_clear(1);
		set_fileupload();
		switchwindow_no_animate('diary');	
		bind_diary_func();
	}
}

function set_fileupload(){
	$('#input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	file_upload()
}
function file_upload(){
	$('.fileupload').fileupload();
	$('.fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#strImgLinkDiary").val(v.name);
					$(".img-diary").html(html);			
					//$(".diary-btn-next").show();					
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}